package com.example.springboot.controller;

import com.example.springboot.domain.PageBean;
import com.example.springboot.domain.Role;
import com.example.springboot.dto.MenusDTO;
import com.example.springboot.enums.ResCodeEnum;
import com.example.springboot.services.RoleService;
import com.example.springboot.vo.ResVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(value = "/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    @GetMapping(value = "/getRoleList")
    public ResVO<PageBean<Role>> getRoleList(HttpServletRequest request){

        int page = Integer.parseInt(request.getParameter("page"));
        int pagesize =  Integer.parseInt(request.getParameter("pagesize"));

        PageBean<Role> rolePageBean = roleService.queryAllRole(page,pagesize);

        return new ResVO<>(ResCodeEnum.SELECTOK.getResCode(),"查询成功",rolePageBean);
    }

    @GetMapping(value = "/getMenuIds")
    public ResVO<List<Integer>> getMenuIds(HttpServletRequest request){

        Integer roleId = Integer.parseInt(request.getParameter("params"));

        List<Integer> integers = roleService.queryRoleMenuIds(roleId);

        return new ResVO<>(ResCodeEnum.SELECTOK.getResCode(),"查询成功",integers);
    }

    @PostMapping(value = "/updatePermission")
    public ResVO<String> updatePermission(@RequestBody MenusDTO menusDTO) {

        if(menusDTO == null ){
            return new ResVO<>(ResCodeEnum.UPDATEER.getResCode(),"授权失败","");
        }
        roleService.updatePermission(menusDTO);

        return new ResVO<>(ResCodeEnum.UPDATEOK.getResCode(), "授权成功","");
    }
}
